import com.nttdocomo.ui.Display;
import com.nttdocomo.ui.Graphics;

public class ChipEditScene extends AbstractScene {
	private int posX;
	private int posY;
	private int curX;
	private int curY;
	private int zoom;
	private ChipDatum chipDatum;

	public ChipEditScene() {
		init();
	}

	public void init() {
		clearLines();
		_scene = -1;

		chipDatum = ChipListScene.getCurrentChip();
		if (chipDatum != null) {
			chipDatum.getPalette()[0] = Graphics.getColorOfRGB(0, 0, 0);
			chipDatum.getPalette()[1] = Graphics.getColorOfRGB(0, 0, 255);
			chipDatum.getPalette()[2] = Graphics.getColorOfRGB(0, 255, 0);
			chipDatum.getPalette()[3] = Graphics.getColorOfRGB(255, 0, 0);
			chipDatum.getPalette()[4] = Graphics.getColorOfRGB(0, 255, 255);
			chipDatum.getPalette()[5] = Graphics.getColorOfRGB(255, 0, 255);
			chipDatum.getPalette()[6] = Graphics.getColorOfRGB(255, 255, 0);
			chipDatum.getPalette()[7] = Graphics.getColorOfRGB(255, 255, 255);
			chipDatum.getPalette()[8] = Graphics.getColorOfRGB(0, 0, 128);
			chipDatum.getPalette()[9] = Graphics.getColorOfRGB(0, 128, 0);
		}

		zoom = 8;
		curX = 20;
		curY = 20;

		dirty = true;
	}

	public void paint(Graphics g) {
		chipDatum.draw(g, posX, posY, zoom);
		dirty = false;
	}

	public void processEvent(int type, int param) {
		if (type == Display.KEY_PRESSED_EVENT) {
			if (param == Display.KEY_UP) {
				if (curY > 0) {
					curY--;
				}
			}
			if (param == Display.KEY_DOWN) {
				if (curY < chipDatum.getChipHeight() - 1) {
					curY++;
				}
			}
			if (param == Display.KEY_LEFT) {
				if (curX > 0) {
					curX--;
				}
			}
			if (param == Display.KEY_RIGHT) {
				if (curX < chipDatum.getChipWidth() - 1) {
					curX++;
				}
			}
			if (Display.KEY_0 <= param && param <= Display.KEY_9) {
				chipDatum.setPixelColor(curX, curY, param);
				dirty = true;
			}
			if(param == Display.KEY_SOFT1){
				_scene = 1;
			}
		}else{
			return;
		}
	}

}
